﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using SpLocalizer.Common;

namespace SpLocalizer.TranslationProvider
{
    public abstract class AbstarctTranslationProvider : ITranslationProvider
    {


        public abstract string TranslateString(string SourceText, CultureInfo FromLanguage,
            CultureInfo ToLanguage, string Format);

        public abstract string[] TranslateArray(string[] SourceText, CultureInfo FromLanguage,
            CultureInfo ToLanguage);

        public BaseTranslationTask Translate(BaseTranslationTask task)
        {
            // Look first if we have already translated tasks in the database
            string Translation = task.LookupForTranslations();

            if (String.IsNullOrEmpty(Translation))
            {
                if (String.IsNullOrEmpty(task.Format) || Constants.TASK_FORMAT_TEXT.Equals(task.Format) || Constants.TASK_FORMAT_HTML.Equals(task.Format))
                {
                    Translation = TranslateString(task.SourceText,
                        task.FromLanguage,
                        task.ToLanguage,
                        task.Format);
                }
                else 
                {
                    IFileTranslator FileTranslator = ServiceContainer.Resolve<IFileTranslator>(task.Format) as IFileTranslator;
                    Translation = FileTranslator.TranslateFile(this, task.SourceText, task.FromLanguage, task.ToLanguage);
                }
            }


            task.TranslatedText = Translation;
            task.Status = TranslationTaskStatus.Translated;

            return task;
        }

    }
}
